package cn.jdy.audit.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import com.itextpdf.text.pdf.languages.ArabicLigaturizer;

import cn.jdy.audit.entity.EmailRecord;
import cn.jdy.audit.entity.SeRecord;
import cn.jdy.common.constant.BankConstant;
import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.payment.entity.Orders;
import cn.jdy.platform.service.ConfService;


/**
 * @title 大数据搜索后台操作
 * @author jga
 * @since 1-11-2016
 */
@Service
public class SeRecordService extends BaseDao<SeRecord>{

  @Autowired
  private ConfService confService;
  
  /**
   * 按条件查询
   * @param sp
   * 
   * @return
   */
  public List<SeRecord> query(SeRecord sp){
    if(sp==null) return super.findAll();
    if(sp.getiDisplayLength() > 0){
       return super.find(createFilter(sp),sp.getiDisplayStart(),sp.getiDisplayLength());
      }
    return super.find(createFilter(sp));
  }
   public List<SeRecord> querypage(SeRecord sp){
         return super.find(createFilter(sp),sp.getPageNo(),18);
    }
  public Long count(SeRecord sp){
    return super.count(createFilter(sp));
  }
  /**
   * 拼装条件
   * @param sp
   * 
   * @return
   */
  private DetachedCriteria createFilter(SeRecord sp){
    DetachedCriteria dc = DetachedCriteria.forClass(entityClass);

    if(!StringUtil.isNullOrEmpty(sp.getCreateTime())&&!",".equals(sp.getCreateTime())) {
      if( sp.getCreateTime().split(",").length>1){
        dc.add(Restrictions.between("createTime", sp.getCreateTime().split(",")[0], sp.getCreateTime().split(",")[1]));
      }else{
        dc.add(Restrictions.like("createTime", sp.getCreateTime(),MatchMode.ANYWHERE));
      }
  }
    if(!StringUtil.isNullOrEmpty(sp.getSe_refer())) {
      dc.add(Restrictions.like("se_refer", sp.getSe_refer(),MatchMode.ANYWHERE));
      }
    if(!StringUtil.isNullOrEmpty(sp.getSe_word())) {
      dc.add(Restrictions.like("se_word", sp.getSe_word(),MatchMode.ANYWHERE));
      }
    if(!StringUtil.isNullOrEmpty(sp.getUser_ip())) {
      dc.add(Restrictions.like("user_ip", sp.getUser_ip(),MatchMode.ANYWHERE));
      }
    if(!StringUtil.isNullOrEmpty(sp.getSe_host())) {
      dc.add(Restrictions.like("se_host", sp.getSe_host(),MatchMode.ANYWHERE));
      }
    if(!StringUtil.isNullOrEmpty(sp.getSe_get())) {
      dc.add(Restrictions.like("se_get", sp.getSe_get(),MatchMode.ANYWHERE));
      }
    dc.addOrder(Order.desc("createTime"));
      return dc;
  }

//统计指标 
  /**
   * 拼装条件
   * @param sp 
   *
   * @return data 拼装对象
   */
  public  List<Map> SeRecordCount(SeRecord sp,String value,int type) {
    String sql="";
        if(type==1){//按年统计
          Calendar a=Calendar.getInstance();
          sql=" select  id,user_ip,se_type,se_word,se_host,se_get,se_refer,add_time,FROM_UNIXTIME(add_time,'%Y') as createTime,se_host,count(se_host)  total FROM  se_record  group by  FROM_UNIXTIME(add_time,'%Y') , "+value ;
        }else if(type==2){//按月统计
          Calendar a=Calendar.getInstance();
          //由于月份是从0开始的所以加1
          sql=" select  id,user_ip,se_type,se_word,se_host,se_get,se_refer,add_time,"
              + "FROM_UNIXTIME(add_time,'%m') as createTime,se_host,count(se_host) "
              + " total FROM  se_record  group by "
              + " FROM_UNIXTIME(add_time,'%m') , "+value+ " "
              + "having FROM_UNIXTIME(add_time,'%Y')=" +String.valueOf(a.get(Calendar.YEAR)-1);

        }else if(type==3){//按季度统计
          sql=" select  id,user_ip,se_type,se_word,se_host,se_get,se_refer,add_time,FROM_UNIXTIME(add_time,'%Y') as createTime,se_host,count(se_host)  total FROM  se_record  group by  FROM_UNIXTIME(add_time,'%Y') , "+value ;

        }else if(type==4){//按某一个月的天
          
        }
    
    SQLQuery query = em().createSQLQuery(sql).addEntity(SeRecord.class);
    return query.list();
  }
  
//统计指标 
  /**
   * 拼装条件
   * @param sp, value 统计字段
   *
   * @return
 
  public  List<Map> SeRecordCountTarget(SeRecord sp,String value) {
    String sql="select  id,user_ip,se_type,se_word,se_host,se_get,se_refer,add_time,FROM_UNIXTIME(add_time,'%Y') as createTime,se_host,count(user_ip) total FROM  se_record  group by   "+value;
    SQLQuery query = em().createSQLQuery(sql).addEntity(SeRecord.class);
    return query.list();
  }  */
  
//统计时间 x轴
  /**
   * 拼装条件
   * @param sp,type 类别
   * 
   * @return
   */
  public  List<Map> SeRecordCountTime(SeRecord sp,int type) {
    List list=new ArrayList();
        if(type==1){//按年统计
          Calendar a=Calendar.getInstance();
          for (int i = a.get(Calendar.YEAR)-4; i < a.get(Calendar.YEAR)+1; i++) {
            list.add(i+"");
          }
        }else if(type==2){//按月统计
          Calendar a=Calendar.getInstance();
          //由于月份是从0开始的所以加1
        
              list.add("01");
              list.add("02");
              list.add("03");
              list.add("04");
              list.add("05");
              list.add("06");
              list.add("07");
              list.add("08");
              list.add("09");
              list.add("10");
              list.add("11");
              list.add("12");
        }else if(type==3){//按季度统计
            for (int i = 0; i < 4; i++) {
              list.add("第"+i+"季度");
            }
        }else if(type==4){//按某一个月的天
          
        }
    return list;
  }
  public void updateSql(){
    String sql="update Se_Record set createTime=FROM_UNIXTIME(add_time) where createTime is NULL";
    SQLQuery sqlQuery = em().createSQLQuery(sql);
    sqlQuery.executeUpdate();
  }
  
  
}
